package com.onluy.conf.interceptor;

import com.alibaba.fastjson.JSON;
import com.onluy.conf.rabbitmq.RabbitmqSender;
import com.onluy.frameworkcloud.tools.utils.DateUtils;
import com.onluy.mqcomsumer.MessageType;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Component
public class SysLogsInterceptor implements HandlerInterceptor {
    protected Logger log =   LoggerFactory.getLogger(this.getClass());


    @Autowired
    private RabbitmqSender rabbitmqSender;


    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView
        modelAndView)
        throws Exception
        {
        }

        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
        throws Exception
        {
        }
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//            Date sysdate=DateUtils.getNow();
//            SysLogs sysLogs=new SysLogs();
//            sysLogs.setUrl(request.getRequestURI()) ;
//            sysLogs.setParams(JSON.toJSONString(request.getParameterMap()) );
//            sysLogs.setMethod(request.getMethod());
//            sysLogs.setEncoding(request.getCharacterEncoding())   ;
//            sysLogs.setIntime(sysdate);
//            sysLogs.setYear(DateUtils.getYear());
//            sysLogs.setMonth(DateUtils.getMonth());
//            sysLogs.setIp(getIpAddr(request));
//
//            try {
//                Subject currentUser = SecurityUtils.getSubject();
//                SysUser sysUser = (SysUser) currentUser.getPrincipal();
//                sysLogs.setUserId(""+sysUser.getUserId());
//                sysLogs.setDepartId(""+sysUser.getDepartId());
//            }catch (Exception ex){
//                log.info("----------- sysLogs : 未登陆"+ex.getMessage()   );
//            }
//
//
//            String message=JSON.toJSONString(sysLogs);
//            log.info("-----------发送mq消息为 : " + message);
//            rabbitmqSender.send(MessageType.log,message);
            //sysLogsService.add(sysLogs);
           return  true;
        }

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

}
